$(document).ready(function(){
    if (!$(".scopes_detail").length) return;

    if ("object" != typeof _detail) _detail = {};

    _detail.loadLightGallery = function(id){
        var $dom = $('#' + id);
        $dom.lightGallery();
        $dom.on('onBeforeOpen.lg',function(event){
            $("body").attr('style', 'overflow:hidden');
            $("#" + id + " img.lazy").each(function(){
                var original = $(this).attr('data-little');
                var src = $(this).attr('src');
                if (original !== src) {
                    $(this).attr('src', original);
                }
            });
        });
        $dom.on('onBeforeClose.lg',function(event){
            $(".cn-img").each(function(){
                var original = $(this).attr('data-little');
		        var src = $(this).attr('src');
                if (original == src) {
                    $(this).attr('src', $(this).attr('data-original'));
                    $("img.lazy").lazyload({
                        effect : "fadeIn",
                        threshold : 200
                    });
                }
            });

            $("body").attr('style', 'overflow:auto');
            var id = $(".lbl-read").attr('data-id'),
                token = $(".lbl-read").attr('data-token');

            $.ajax({
                url: '/detail/' + id + '/visit',
                type: 'POST',
                beforeSend: function(xhr){
                    xhr.setRequestHeader('X-CSRF-TOKEN', token);
                },
                data: {
                },
                dataType: "json",
            }).done(function(data){
            })
            .fail(function(error){
            });
        });
    };

    _detail.loadLightGallery('lightgallery_ee');
    _detail.loadLightGallery('lightgallery_new');
});
